package com.imageprocess.tools;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

/**
 * 对SD卡文件的管理
 * 
 * @author ch.linghu
 * 
 */
public class FileHelper {
    private static final String TAG = "FileHelper";

    public static final String BASE_PATH = "imageprocess";
    public static final String SAVEFILE_NAME = BASE_PATH + "/save/";

    public static String getSaveImagePath(String filename) {
        return Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/" + SAVEFILE_NAME + filename;
    }

    public static void createSaveFolder() throws IOException {
        File basePath = new File(Environment.getExternalStorageDirectory(),
                BASE_PATH + "/save/");
        if (!basePath.exists()) {
            if (!basePath.mkdirs()) {
                throw new IOException(String.format("%s cannot be created!",
                        basePath.toString()));
            }
        }
    }

    public static File getBasePath() throws IOException {
        File basePath = new File(Environment.getExternalStorageDirectory(),
                BASE_PATH);

        if (!basePath.exists()) {
            if (!basePath.mkdirs()) {
                throw new IOException(String.format("%s cannot be created!",
                        basePath.toString()));
            }
        }

        if (!basePath.isDirectory()) {
            throw new IOException(String.format("%s is not a directory!",
                    basePath.toString()));
        }

        return basePath;
    }

    public static boolean fileExists(String name) {
        File saveFile = new File(Environment.getExternalStorageDirectory(),
                SAVEFILE_NAME + name);
        return saveFile.exists();
    }

    public static boolean saveImage() {
        return true;
    }

    public static void copyFile(File src, File tar) throws Exception

    {

        if (src.isFile())

        {

            InputStream is = new FileInputStream(src);

            OutputStream op = new FileOutputStream(tar);

            BufferedInputStream bis = new BufferedInputStream(is);

            BufferedOutputStream bos = new BufferedOutputStream(op);

            byte[] bt = new byte[8192];

            int len = bis.read(bt);

            while (len != -1)

            {

                bos.write(bt, 0, len);

                len = bis.read(bt);

            }

            bis.close();

            bos.close();

        }

        if (src.isDirectory())

        {

            File[] f = src.listFiles();

            tar.mkdir();

            for (int i = 0; i < f.length; i++)

            {

                copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile()
                        + File.separator + f[i].getName()));

            }

        }

    }

}
